var mslibrary = require('ms-common-library');
var ajax = require('./ajax');
var path = require('path');
module.exports = function (UC, source, config, ArFor, cache, package) {
  UserControl = UC;
  var router = function (req, res, next) {
    var jsfile = path.join(__dirname, './../drive/sw-clinet.js');
    mslibrary.openfile(jsfile, function (error, val) {
      if (error) {
        val = 'var val=1';
      }
      var appname = 'app';
      if (req.params.type === "admin") {
        appname = 'admin';
      }

      ajax.GET(config.getdomain(req, '/' + appname + "/manifest.appcache"), {}, function (appcache, error) {
        var cache_list = '[]';
        if (error == null) {
          var strat_a = 'CACHE:';
          appcache = appcache.Replace('../../', '../').Replace('../', './').Replace('././', './').Replace('./', '{dm}/');

          var a = appcache.indexOf(strat_a) + strat_a.length + 1;
          var b = appcache.indexOf('NETWORK') - 1;
          var str = appcache.substring(a, b);

          cache_list = str.split('\n');

          var offlineurl = config.getdomain(req, ("/" + appname + "/"));

          cache_list.push(offlineurl);


          offlineurl = config.getdomain(req, ("/" + appname));

          cache_list.push(offlineurl);

          offlineurl = "{dm}/" + appname + "/";

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!32/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!48/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!96/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!144/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!72/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!128/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!188/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!192/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!256/j.png");

          cache_list.push("{dm}/Service/Images/" + config.icon + "/!512/j.png");

          cache_list.push("{dm}/extendfile/ms-element-ui/js/error-404.json");




          cache_list = JSON.stringify(cache_list)
        }

        val = val.replace("'cache_list'", cache_list)

        val = val.replace('"cache_list"', cache_list)
        appname = appname + '_' + package.name;

        appname = appname.Replace('-', "_");
        var version = package.version + "." + (config.id + "").split('').join('.') + "." + config.version;
        val = val.replace('{version}', version).replace('{name}', appname)

        val = val.replace('-{dm}-', config.getdomain(req, ""));

        res.Js(val)
      })
    });
  }
  return router;
};